<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ConfigurationController extends Controller
{
    /**
    *
    *   修改网站配置页面
    *
    */
    public function getAdd()
    {
        $data = DB::table('configuration') -> first();
        return view('admin.configuration.add',['data'=>$data]);

    }
    
    
   
    public function postUpdate(Request $request)
    {
        // 提取参数
        $arr = $request -> except('_token');

        // 判断图片是否修改
        if ($request->hasFile('logo')) {
            // 获取图片后缀
            $suffix = $request -> file('logo') -> getClientOriginalExtension();
            //设置图片名称 
            $name = date('YmdHis',time());

            // 判断图片后缀名
            $array = ['jpg','png','gif','jpeg'];
            if (in_array($suffix,$array)) {
                $arr['logo'] = '/ad/upload/'.$name.'.'.$suffix;
                $request -> file('logo') -> move('./ad/upload',$name.'.'.$suffix);
            }else{
               return back() -> with('error','文件格式不正确');
            }
        }
        // 修改数据
        $res = DB::table('configuration') -> update($arr);

        // 处理结果
        if ($res > 0) {
            return redirect('/admin/configuration/add') -> with('success','修改成功');
        }else{
            return back() -> with('error','修改失败');
        }
    }
}
